#ifndef FORMMODEL_H
#define FORMMODEL_H

#include <QObject>
#include <QHash>
#include <functional>

class QLineEdit;
class QTextEdit;
class QPlainTextEdit;
class QDateTimeEdit;
class QComboBox;
class QSpinBox;
class QDoubleSpinBox;
class QCheckBox;
class QTableWidget;
class QTableWidgetItem;

namespace Qy{
class Table;

typedef std::function<QVariant(const QVariant&, QWidget*)> FormModelFilter;

enum FormModelType{
    QLineEditType = 0, QTextEditType, QPlainTextEditType, QDateTimeEditType,
    QComboBoxType, QSpinBoxType, QDoubleSpinBoxType, QCheckBoxType, QTableWidgetItemType
};
class FormModel : public QObject
{
    Q_OBJECT

public:
    explicit FormModel(Table* table, QObject *parent = nullptr);
    void connect(QWidget* inputWidget, const QString& fieldName, FormModelType type, const QString& QDateTimeForceStringFormat = "");
    void connect(QTableWidget* table, QTableWidgetItem* item, const QString &fieldName);
    void getData(const QString &fieldName, bool directChange);
    void getData();
    void getChangedData();
    void setData();
    void setReadOnly(bool isReadOnly = true);

    bool hasChange();

    template<class Type>inline Type* getTable(){
        static_assert(std::is_base_of<Table, Type>::value, "T must base of Table!");
        return dynamic_cast<Type*>(table);
    }

signals:

public slots:
public:
    Table* table;
    QStringList listFieldName;
    QHash<QString, QWidget*> hashInputWidget;
    QHash<QString, QTableWidgetItem*> hashTableItem;
    QHash<FormModelType, FormModelFilter> hashGetFilter;
    QHash<FormModelType, FormModelFilter> hashSetFilter;
};

}
#endif // FORMMODEL_H
